

package com.hazelcast.client.impl;

import com.hazelcast.internal.util.ThreadLocalRandomProvider;

import java.security.SecureRandom;
import java.util.Arrays;

/**
 * Represents the secure-randomly generated tokens associated
 * to the clients so that the client can proof its identity
 * while authenticating with the TPC channels.
 */
public final class TpcToken {
    private static final int CONTENT_LENGTH = 64;
    private final byte[] content;

    public TpcToken() {
        SecureRandom secureRandom = ThreadLocalRandomProvider.getSecure();
        byte[] content = new byte[CONTENT_LENGTH];
        secureRandom.nextBytes(content);
        this.content = content;
    }

    /**
     * @return The content of the token.
     */
    public byte[] getContent() {
        return content;
    }

    /**
     * @param tokenContent Token to check.
     * @return Whether the given token matches with this or not.
     */
    public boolean matches(byte[] tokenContent) {
        return Arrays.equals(this.content, tokenContent);
    }
}
